package com.cskaoyan.wx.service;

import com.cskaoyan.bean.Comment;
import com.cskaoyan.bean.CommentExample;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.UserExample;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.wx.bean.vo.CommentVoCL.CommentCountVo;
import com.cskaoyan.wx.bean.vo.CommentVoCL.CommentData;
import com.cskaoyan.wx.bean.vo.CommentVoCL.DataSonVo;
import com.cskaoyan.wx.bean.vo.CommentVoCL.UserInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * 功能:评论
 * 作者:chen11
 */
@Service
public class WxCommentServiceImpl implements WxCommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public CommentData SelectCommentList(Integer valueId, Integer type, Integer size, Integer page, Integer showType) {

        PageHelper.startPage(page,size);
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andValueIdEqualTo(valueId);
        List<Comment> comments = commentMapper.selectByExample(commentExample);

        CommentData data = new CommentData();
        data.setCount(comments.size());
        data.setCurrentPage(1);
        List<DataSonVo>dataSonVo = new ArrayList<>();

        for(Comment comment:comments){
            DataSonVo dataSonVo1 = new DataSonVo();

            //userInfo添加进去
            UserInfo userInfo = new UserInfo();
            Integer userId = comment.getUserId();
            UserExample userExample = new UserExample();
            userExample.createCriteria().andIdEqualTo(userId);
            List<User> users = userMapper.selectByExample(userExample);
            userInfo.setAvatarUrl(users.get(0).getAvatar());
            userInfo.setNickName(users.get(0).getNickname());
            dataSonVo1.setUserInfo(userInfo);
            dataSonVo.add(dataSonVo1);

            dataSonVo1.setAddTime(comment.getAddTime());
            dataSonVo1.setContent(comment.getContent());
            dataSonVo1.setPicList(comment.getPicUrls());

            dataSonVo.add(dataSonVo1);
        }
        data.setData(dataSonVo);
        System.out.println(data);
        return data;
    }

    @Override
    public CommentCountVo CountComment(Integer valueId, Integer type) {
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andValueIdEqualTo(valueId).andTypeEqualTo(type).andHasPictureEqualTo(true);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        long hasPic = commentPageInfo.getTotal();

        CommentExample commentExample2 = new CommentExample();
        commentExample2.createCriteria().andValueIdEqualTo(valueId).andTypeEqualTo(type);
        List<Comment> comments2 = commentMapper.selectByExample(commentExample);
        PageInfo<Comment> commentPageInfo2 = new PageInfo<>(comments);
        long allcount = commentPageInfo2.getTotal();

        CommentCountVo commentCountVo = new CommentCountVo();
        commentCountVo.setAllCount((int) allcount);
        commentCountVo.setHasPicCount((int) hasPic);
        System.out.println(allcount+" "+hasPic);
        return commentCountVo;

    }
}
